package top.kaoshanji.p1ch10d;

import top.kaoshanji.p1ch09a.Note;

/**
 * 代码清单-p1ch10d-235页a：乐器示例第5版
 * author kaoshanji
 * ^_^
 * create time 2025/3/10 18:10
 */
public class Music5 {

    static void tune(Instrument i) {
        i.play(Note.MIDDLE_C);
    }

    static void tuneAll(Instrument[] e) {
        for(Instrument i : e) {
            tune(i);
        }
    }

    public static void main(String[] args) {

        // 填充数组时会自动向上转型
        Instrument[] orchestra = {new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind()};

        tuneAll(orchestra);
    }


}

interface Instrument {

    // 编译时常量
    int VALUE = 5; // static & final

    // 自动就是 public 的
    default void play(Note n) {
        System.out.println(this + ".play() " + n);
    }

    default void adjust() {
        System.out.println("Adjusting " + this);
    }

}

class Wind implements Instrument {
    @Override
    public String toString() {
        return "Wind{}";
    }
}


class Percussion implements Instrument {
    @Override public String toString() {
        return "Percussion";
    }
}

class Stringed implements Instrument {
    @Override public String toString() {
        return "Stringed";
    }
}

class Brass extends Wind {
    @Override public String toString() {
        return "Brass";
    }
}

class Woodwind extends Wind {
    @Override public String toString() {
        return "Woodwind";
    }
}

